package com.blb.goods_service.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.goods_service.entity.*;
import com.blb.goods_service.service.IBrandService;
import com.blb.goods_service.service.ICategoryService;
import com.blb.goods_service.service.ISpuService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * spu表，该表描述的是一个抽象性的商品，比如 iphone8 前端控制器
 * </p>
 *
 * @author xray
 * @since 2020-09-07
 */
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Autowired
    private ISpuService spuService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/page/{page}")
    public JsonResult<PageResult<Spu>> page(@PathVariable("page")Integer page){
        try {
            Page<Spu> spuPage = spuService.page(new Page<>(page, 10),
                    new QueryWrapper<Spu>().lambda().eq(Spu::getValid,true));
            spuPage.getRecords().forEach(spu -> {
                Category c1 = categoryService.getById(spu.getCid1());
                Category c2 = categoryService.getById(spu.getCid2());
                Category c3 = categoryService.getById(spu.getCid3());
                spu.setCategory(c1.getName()+"/"+c2.getName()+"/"+c3.getName());
                Brand brand = brandService.getById(spu.getBrandId());
                spu.setBrand(brand.getName());
            });
            return new JsonResult(1,new PageResult<Spu>(
                    spuPage.getCurrent(),
                    spuPage.getTotal(),
                    spuPage.getSize(),
                    spuPage.getRecords()));
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @PostMapping
    public JsonResult saveSpu(@RequestBody Spu spu){
        try {
            spuService.saveSpu(spu);
            //发送消息到MQ 参数1 是交换机名称 参数2 是key 参数3 是数据
            rabbitTemplate.convertAndSend("blb.mall.exchange","goods.save",spu.getId());
            return new JsonResult(1,spu);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @DeleteMapping("/{spuId}")
    public JsonResult deleteSpu(@PathVariable("spuId")Long spuId){
        try {
            spuService.removeById(spuId);
            //发送消息到MQ 参数1 是交换机名称 参数2 是key 参数3 是数据
            rabbitTemplate.convertAndSend("blb.mall.exchange","goods.delete",spuId);
            return new JsonResult(1, null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @GetMapping("/{spuId}")
    public JsonResult<Spu> findSpuById(@PathVariable("spuId")Long spuId){
        try{
            Spu spu = spuService.findById(spuId);
//            Spu spu = spuService.getById(spuId);
            return new JsonResult<>(1,spu);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }
}

